<?php
namespace app\shop\controller;
use app\admin\controller\Admin;
use app\AdminRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\shop\model\AdminShopCollageList as ShopCollageListModel;
use app\shop\model\AdminShopCollage as collageModel;
use app\shop\model\AdminShopSpePrice;
use app\shop\model\AdminGoods;

class AdminShopCollage extends AdminRest
{

    protected $model;
    protected $collage_model;
    protected $spe_price_model;
    protected $goods_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model           = new ShopCollageListModel();
        $this->collage_model   = new collageModel();
        $this->spe_price_model = new AdminShopSpePrice();
        $this->goods_model     = new AdminGoods();

    }
    /**
     * 获取拼团记录列表
     */
    public function collageList(){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['collage_status','in','1,2,3,4'];
        $page  = $this->_input['limit'];
        $data  = $this->model->collageList($dis,$page);
        return $this->success($data);
    }
    /**
     * 获取商品拼团设置信息列表
     */
    public function goodsCollageList(){
        $input = $this->_input;
        $dis[] = ['a.goods_id','=',$input['goods_id']];
        $dis[] = ['a.status','=',1];
        $data  = $this->collage_model->collageList($dis,$input['limit']);
        if(!empty($data['data'])){
            foreach ($data['data'] as &$v){
                $v['spe_content'] = $v['spe_text']['spe_name_text'].':'.$v['spe_text']['price'];
            }
        }
        return $this->success($data);
    }
    /**
     *
     * 添加商品拼团
     */

    public function goodsCollageAdd(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
//        dump($input);exit;
        $res = $this->collage_model->collageAdd($input);

        $res = $this->goods_model->goodsUpdate(['id'=>$input['goods_id']],['is_collage'=>1]);
        return $this->success($res);
    }

    /**
     *
     * 编辑商品拼团
     */
    public function goodsCollageUpdate(){
        $input = $this->_input;
        $dis[] = ['id','=',$input['id']];
        if(count($input)>1){
            $res = $this->collage_model->collageUpate($dis,$input);
            if(!empty($input['status'])&&$input['status']==-1){
                $this->checkCollageGoods($input['goods_id']);
            }else{
                 $this->goods_model->goodsUpdate(['id'=>$input['goods_id']],['is_collage'=>1]);
            }
        }else{
            $res = $this->collage_model->collageInfo($dis);
        }
        return $this->success($res);
    }
    /**
     *
     * 判断是否是拼团商品
     *
     */
    public function checkCollageGoods($goods_id){
        $data = $this->collage_model->collageInfo(['goods_id'=>$goods_id,'status'=>1]);
        if(empty($data)){
            $this->goods_model->goodsUpdate(['id'=>$goods_id],['is_collage'=>0]);
        }
        return true;
    }
    /**
     *
     * 获取规格
     */
    public function getSpePrice(){
        $input = $this->_input;
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['goods_id','=',$input['goods_id']];
        $dis[] = ['status','>',-1];
        $data  = $this->spe_price_model->goodsSpePrice($dis);
        if(!empty($data)){
            foreach ($data as &$v){
                $v['spe_content'] = $v['spe_name_text'].':'.$v['price'];
                $v['spe_price_id']= $v['id'];
            }
        }
        return $this->success($data);
    }


}
